package timer;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.youlai.system.mqtt.MqttSubClient;
import com.youlai.system.pojo.entity.device.WxMacRelEntity;
import com.youlai.system.service.impl.device.WxMacRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class SendInfoToDeviceTimer {
    private WxMacRelService wxMacRelService;

    @Autowired
    private MqttSubClient mqttSubClient;

    @Scheduled(fixedDelay = 1000L, fixedRate = 5 * 1000L)
    public void doSend() {
        List<WxMacRelEntity> wxMacRelEntityList = wxMacRelService.list();
        for(WxMacRelEntity wxMacRelEntity : wxMacRelEntityList){
            Map<String,String> data = Maps.newHashMap();
            data.put("ip_address",wxMacRelEntity.getServer());
            data.put("port_number",wxMacRelEntity.getPort().toString());
            mqttSubClient.send("/down/tcp_ip/"+wxMacRelEntity.getMac(),
                    JSON.toJSONString(data));
        }
    }
}
